<?php

class AdminCTR extends Controller
{
    function init()
    {
        $ConfigModel = new Model('config');
        $config = $ConfigModel->fetch();
        $this->assign('config', $config);
        if ($_GET['a'] != 'login' && !Session::get('isLogin')) {
            $this->redirect($this->urlCreate('admin', 'login'));
        }
    }

    function index()
    {
        if (Session::get('isLogin')) {
            $this->assign('title', 'DoBBS Manage Panel');
            $this->display();
        }
    }

    function login()
    {
        $tip = '';
        if ($this->isPost()) {
            $user = $_POST['user'];
            $pass = md5($_POST['pass']);
            $UserModel = new Model('user');
            $admin = $UserModel->fetch('*', array('id' => 1));
            if (!empty($admin) && $user === $admin['user'] && $pass === $admin['pass']) {
                Session::set('isLogin', true);
                $this->success('验证成功，欢迎回来', $this->urlCreate('admin', 'index'));
                exit;
            } else {
                $tip = '账号密码错误';
            }
        }

        $this->assign('title', '站长登录 - DoBBS');
        $this->assign('tip', $tip);
        $this->display();
    }

    function logout()
    {
        Session::del('isLogin');
        $this->success('操作成功', $this->urlCreate('index', 'index'));
    }

    function config()
    {
        $ConfigModel = new Model('config');
        if ($this->isPost()) {
            $title = $_POST['title'];
            $name = $_POST['name'];
            $desc = $_POST['desc'];
            $keywd = $_POST['keywd'];
            $url = $_POST['url'];
            $vip = $_POST['vip'];
            $uppre = $_POST['uppre'];
            if ($ConfigModel->update(array(
                'title' => $title,
                'name' => $name,
                'desc' => $desc,
                'keywd' => $keywd,
                'global_head' => '',
                'global_foot' => '',
                'url' => $url,
                'vip' => $vip,
                'upload_prefix' => $uppre))) {
                $this->success('更新成功', $this->urlCreate('admin', 'config'));
                exit;
            } else {
                $this->error('更新失败，或许是您未做改动', $this->urlCreate('admin', 'index'));
                exit;
            }
        }
        $config = $ConfigModel->fetch();
        $this->assign('config', $config);
        $this->assign('title', '系统配置 - DoBBS Manage Panel');
        $this->display();
    }

    function forum()
    {
        $ForumModel = new Model('forum');
        $tip = '';
        if ($this->isPost()) {
            $name = trim($_POST['name']);
            if ($name) {
                if ($ForumModel->insert(array('name' => $name))) {
                    $this->success('操作成功', $this->urlCreate('admin', 'forum'));
                } else {
                    $tip = '操作失败，请重试';
                }
            } else {
                $tip = '论坛名不能为空';
            }
        }
        $this->assign('tip', $tip);
        $forums = $ForumModel->fetchAll();
        $this->assign('forums', $forums);
        $this->assign('title', '论坛管理 - DoBBS Manage Panel');
        $this->display();
    }

    function forum_delete()
    {
        $id = $_GET['id'] + 0;
        $ForumModel = new Model('forum');
        if ($id && $ForumModel->delete(array('id' => $id))) {
            $this->success('删除成功', $this->urlCreate('admin', 'forum'));
        } else {
            $this->error('操作失败，请重试', $this->urlCreate('admin', 'forum'));
        }
    }

    function forum_change()
    {
        $tip = '';
        $id = $_GET['id'] + 0;
        $ForumModel = new Model('forum');
        $forum = $ForumModel->fetch('*', array('id' => $id));
        if ($this->isPost()) {
            $name = $_POST['name'];
            if ($name && $ForumModel->update(array('name' => $name), array('id' => $id))) {
                $this->success('操作成功', $this->urlCreate('admin', 'forum'));
            } else {
                $tip = '操作失败，或许是您未做改动';
            }
        }
        $this->assign('tip', $tip);
        $this->assign('forum', $forum);
        $this->assign('title', '论坛修改 - DoBBS Manage Panel');
        $this->display();
    }

    function section()
    {
        $tip = '';
        $id = $_GET['id'] + 0;
        $ForumModel = new Model('forum');
        $forum = $ForumModel->fetch('*', array('id' => $id));
        $SectionModel = new Model('section');
        if ($this->isPost()) {
            $name = trim($_POST['name']);
            if ($name) {
                if ($SectionModel->insert(array('name' => $name, 'fid' => $id))) {
                    $this->success('操作成功', $this->urlCreate('admin', 'section', array('id' => $id)));
                } else {
                    $tip = '操作失败，请重试';
                }
            } else {
                $tip = '版块名不能为空';
            }
        }
        $this->assign('tip', $tip);
        $sections = $SectionModel->fetchAll('*', array('fid' => $id));
        $this->assign('sections', $sections);
        $this->assign('forum', $forum);
        $this->assign('title', '版块管理 - DoBBS Manage Panel');
        $this->display();
    }

    function section_delete()
    {
        $id = $_GET['id'] + 0;
        $SectionModel = new Model('section');
        if ($id && $SectionModel->delete(array('id' => $id))) {
            $this->success('删除成功', Client::getRef());
        } else {
            $this->error('操作失败，请重试', 'javascript:history.go(-1);');
        }
    }

    function section_change()
    {
        $tip = '';
        $id = $_GET['id'] + 0;
        $SectionModel = new Model('section');
        $section = $SectionModel->fetch('*', array('id' => $id));

        $ForumModel = new Model('forum');
        $forums = $ForumModel->fetchAll();

        if ($this->isPost()) {
            $fid = $_POST['parent'] + 0;
            $name = $_POST['name'];
            if ($fid && $name && $SectionModel->update(array('name' => $name, 'fid' => $fid),
                array('id' => $id))) {
                $this->success('操作成功', $this->urlCreate('admin', 'forum'));
            } else {
                $tip = '操作失败，请重试';
            }
        }
        $this->assign('tip', $tip);
        $this->assign('section', $section);
        $this->assign('forums', $forums);
        $this->assign('title', '版块修改 - DoBBS Manage Panel');
        $this->display();
    }

    function topic()
    {
        $sid = $_GET['id'] + 0;
        $page = (isset($_GET['page']) && $_GET['page'] + 0 > 0) ? $_GET['page'] : 1;
        $TopicModel = new Model('topic');
        $pages = $TopicModel->count(array('sid' => $sid));
        $pages = ceil($pages / 10);
        $topics = $TopicModel->select('*')->where("sid='{$sid}'")->order('id', false)->
            limit($page - 1, 10)->fetchAll();
        $this->assign('next_url', $this->urlCreate('admin', 'topic', array('id' => $sid,
                'page' => $page + 1)));
        $this->assign('last_url', $this->urlCreate('admin', 'topic', array('id' => $sid,
                'page' => $page - 1)));
        $this->assign('nowpage', $page);
        $this->assign('pages', $pages);
        $this->assign('topics', $topics);
        $this->assign('title', '主题管理 - DoBBS Manage Panel');
        $this->display();
    }

    function topic_delete()
    {
        $id = $_GET['id'] + 0;
        $TopicModel = new Model('topic');
        if ($id && $TopicModel->delete(array('id' => $id))) {
            $this->success('删除成功', Client::getRef());
        } else {
            $this->error('操作失败，请重试', 'javascript:history.go(-1);');
        }
    }

    function topic_change()
    {
        $tip = '';
        $id = $_GET['id'] + 0;
        $TopicModel = new Model('topic');
        $topic = $TopicModel->fetch('*', array('id' => $id));
        $Topic_stateModel = new Model('topic_state');
        $state = $Topic_stateModel->fetchAll();

        if ($this->isPost()) {
            $name = $_POST['title'];
            $content = $_POST['content'];
            if ($name && $TopicModel->update(array('title' => $name, 'content' => $content),
                array('id' => $id))) {
                $this->success('操作成功', $this->urlCreate('admin', 'topic', array('id' => $id)));
            } else {
                $tip = '操作失败，请重试';
            }
        }
        $this->assign('tip', $tip);
        $this->assign('states', $state);
        $this->assign('topic', $topic);
        $this->assign('superior', $this->urlCreate('admin', 'topic', array('id' => $topic['sid'])));
        $this->assign('title', '话题修改 - DoBBS Manage Panel');
        $this->display();
    }

    function auth()
    {
        $tip = '';
        $AuthModel = new Model('auth');
        if ($this->isPost()) {
            $name = $_POST['name'];
            $value = $_POST['value'] + 0;
            if (!empty($name)) {
                if ($AuthModel->insert(array('name' => $name, 'value' => $value))) {
                    $this->success('操作成功', $this->urlCreate('admin', 'auth'));
                } else {
                    $tip = '操作失败，请重试';
                }
            } else {
                $tip = '请完整填写各项';
            }
        }
        $auths = $AuthModel->fetchAll();
        $this->assign('tip', $tip);
        $this->assign('auths', $auths);
        $this->assign('title', '权限配置 - DoBBS Manage Panel');
        $this->display();
    }

    function auth_delete()
    {
        $id = $_GET['id'] + 0;
        $AuthModel = new Model('auth');
        if ($id && $AuthModel->delete(array('id' => $id))) {
            $this->success('删除成功', Client::getRef());
        } else {
            $this->error('操作失败，请重试', 'javascript:history.go(-1);');
        }
    }

    function announcement()
    {
        $tip = '';
        $AnnouncementModel = new Model('announcement');
        if ($this->isPost()) {
            $title = $_POST['title'];
            $content = $_POST['content'];
            if (!empty($title) && !empty($content)) {
                if ($AnnouncementModel->insert(array(
                    'title' => $title,
                    'content' => $content,
                    'ptime' => time()))) {
                    $this->success('发布成功', $this->urlCreate('admin', 'announcement'));
                } else {
                    $tip = '发布失败，请重试';
                }
            } else {
                $tip = '请完整填写各项';
            }
        }
        $announcements = $AnnouncementModel->fetchAll();
        $this->assign('tip', $tip);
        $this->assign('announcements', $announcements);
        $this->assign('title', '公告管理 - DoBBS Manage Panel');
        $this->display();
    }

    function announcement_delete()
    {
        $id = $_GET['id'] + 0;
        $AnnouncementModel = new Model('announcement');
        if ($id && $AnnouncementModel->delete(array('id' => $id))) {
            $this->success('删除成功', Client::getRef());
        } else {
            $this->error('操作失败，请重试', 'javascript:history.go(-1);');
        }
    }

    function links()
    {
        $tip = '';
        $LinksModel = new Model('links');
        if ($this->isPost()) {
            $title = $_POST['title'];
            $url = $_POST['url'];
            if (!empty($title) && !empty($url)) {
                if ($LinksModel->insert(array(
                    'title' => $title,
                    'url' => $url,
                    'ptime' => time()))) {
                    $this->success('添加成功', $this->urlCreate('admin', 'links'));
                } else {
                    $tip = '增加失败，请重试';
                }
            } else {
                $tip = '请完整填写各项';
            }
        }
        $links = $LinksModel->fetchAll();
        $this->assign('tip', $tip);
        $this->assign('links', $links);
        $this->assign('title', '友链管理 - DoBBS Manage Panel');
        $this->display();
    }

    function links_delete()
    {
        $id = $_GET['id'] + 0;
        $LinksModel = new Model('links');
        if ($id && $LinksModel->delete(array('id' => $id))) {
            $this->success('删除成功', Client::getRef());
        } else {
            $this->error('操作失败，请重试', 'javascript:history.go(-1);');
        }
    }
}

?>